<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-yellow sidebar-mini">

<section class="content-header">
    <h1>制作件清单分配</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label for="query-proCode">项目编号</label>
                                    <input id="query-proCode" type="text" name="proCode" class="form-control"
                                           placeholder="请输入项目编号" >
                                </div>
                                <div class="form-group">
                                    <label for="query-proName">项目名称</label>
                                    <input id="query-proName" type="text" name="proName" class="form-control"
                                           placeholder="请输入项目名称">
                                </div>
                                <div class="form-group">
                                    <label for="query-malType">类型</label>
                                    <select id="query-malType" class="form-control" name="special">
                                        <option value="" selected>请选择</option>
                                        <option th:each="type : ${special}" th:value="${type.name}"
                                                th:text="${type.name}"></option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="query-part">部分</label>
                                    <input id="query-part" type="text" name="part" class="form-control"
                                           placeholder="请输入部分">
                                </div>
                                <div class="form-group">
                                    <label for="query-sheetType">提报类型</label>
                                    <select id="query-sheetType" class="form-control" name="sheetType">
                                        <option value="" selected>请选择</option>
                                        <option value="1">提报单</option>
                                        <option value="2">变更单</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="query-status">执行状态</label>
                                    <select id="query-status" class="form-control" name="lssueStatus">
                                        <option value="">请选择</option>
                                        <option value="1" selected>未执行</option>
                                        <option value="2">已执行</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="query-branchName">提报科室</label>
                                    <input id="query-branchName" type="text" name="sectionName" class="form-control">
                                </div>
                                <div class="form-group">
                                    <label for="query-postName">提报人</label>
                                    <input id="query-postName" type="text" name="postName" class="form-control">
                                </div>
<!--                                <div class="form-group">-->
<!--                                    <label for="query-startTime">日期</label>-->
<!--                                    <input id="query-startTime" type="text" class="form-control datepicker" name="startTime"-->
<!--                                           readonly="readonly"/>-->
<!--                                    <label for="query-endTime"> 至 </label>-->
<!--                                    <input id="query-endTime" type="text" class="form-control datepicker" name="endTime"-->
<!--                                           readonly="readonly"/>-->
<!--                                </div>-->
                                <button type="button" class="btn btn-success" id="btn-search"
                                        onclick="js.table.search(table)">
                                    <span class="Bold">搜索</span>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>

                <div class="form-inline">
                    <div class="form-group">
                        <label for="query-unproNum">未下达计划项目数：</label>
                        <input id="query-unproNum" type="text" class="form-control"
                               style="border: none;width: 200Px;background-color: white" readonly
                        th:value="${sumNum.unproNum}">
                    </div>
                    <div class="form-group">
                        <label for="query-unplanNum">未下达计划数：</label>
                        <input id="query-unplanNum" type="text" class="form-control"
                               style="border: none;width: 300px;background-color: white" readonly
                        th:value="${sumNum.unplanNum}">
                    </div>
                </div>

                <div class="box-body">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    let table, sheetType = ['','提报单','变更单'];

    $(function () {

        /** 加载列表 */
        table = js.table.init({
            url: ctx + "tecmake/list",
            showExport: false,
            columns: [
                {
                    title: '序号', field: 'id', width: '50',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: "项目编号", field: 'prodNo',

                    formatter:function (value,row){
                   ;
                        if(value){
                            return '<span>'+value+'</span>';
                        }else{
                            return '<span>'+row.proCode+'</span>';

                        }
                    }
                },
                {title: "项目名称", field: 'proName',width:'250px'},
                {title: "项目数量", field: 'proNum'},
                {title: "类型", field: 'special'},
                {title: "部分", field: 'part'},
                {title: "提报类型", field: 'sheetType',
                    formatter: function (value) {
                        return value == 1 ? '提报单' : '变更单';
                    }
                },
                {title: "提报人", field: 'postName'},
                {title: "提报科室", field: 'sectionName'},
                {title: "计划数量", field: 'unplanNum'},
                {title: "提报日期", field: 'createTime',width:'100px'},
                {title: "执行状态", field: 'lssueStatus',
                    formatter: function (value) {
                        if (value === 1) {
                            return '<span class="badge label-inverse">未执行</span>';
                        } else if (value === 2) {
                            return '<span class="badge label-warning">已执行</span>';
                        }
                    }
                },
                {title: "操作",
                    formatter: function (value, row) {
                        let actions = [];
                        if(row.sheetType==2){
                            if(row.prodNoNum>1){
                                actions.push('<a class="btn btn-xs" onclick="changeMakeSplit('+js.jsonStringify(row)+')"> 变更拆分</a> ');
                            }
                            if(row.prodNoNum==1&&row.lssueStatus==1){
                                actions.push('<a class="btn btn-primary btn-xs" onclick="chaMakeRelease('+js.jsonStringify(row)+')"> 变计划下达</a> ');
                            }
                        }
                        if(row.prodNoNum >1 && row.sheetType==1){
                            actions.push('<a class="btn btn-warning btn-xs" href="' + ctx + 'tecmake/split/' + row.id + '"> 拆分</a> ');
                        }else if(row.prodNoNum==1 && row.sheetType==1 && row.lssueStatus==1) {
                            actions.push('<a class="btn btn-primary btn-xs" target="_blank" onclick="planRelease('+js.jsonStringify(row)+')"> 计划下达</a> ');
                        }
                        actions.push('<a class="btn btn-info btn-xs" target="_blank" onclick="selectDetail('+js.jsonStringify(row)+')">查看</a> ');

                        return actions.join('');
                    }
                }
            ]
        });

    });

/** 根据生产令拆分状态查看不同的制作件清单详情*/
 function selectDetail(row){

    if(row.prodNo){
        window.open(ctx + 'tecmake/selectResult?prodNo='+ row.prodNo);
    }else{
        window.open(ctx + 'tecmake/selectDetail?id=' + row.id + '&sheetType='+row.sheetType);
    }
}


/** 计划下达*/
function planRelease(row){
    let url;//访问的url
    let resultAndSheetId;//主表的是主表Id，输出表是输出表的Id
    let prodNo;//生产令号
    /*联查标记,1=sheet表数据，2=result表数据*/
    if(row.meter==1){
        url = ctx + "tecmake/sheetRelease";
        resultAndSheetId = row.id;
        prodNo = row.prodNo;
    }else if(row.meter==2){
        url = ctx + "tecmake/resultRelease";
        resultAndSheetId = row.resultId;
        prodNo = row.prodNo;

    }
    js.confirmPost({
        content:"确定下达此任务吗?",
        url: url,
        async: false,
        data:{
            resultAndSheetId:resultAndSheetId,
            prodNo:prodNo,
        },
        success: function (result) {
            if (result.type === web_status.SUCCESS) {
                js.modal.success(result.msg)
                js.table.refresh(table);
                refreshNum();
            } else {
                js.modal.warning(result.msg);
            }
        }
    });

}

/** 变更单的下达数据*/
function chaMakeRelease(row){
        debugger;
        let url;//访问的url
        /*联查标记,1=sheet表数据，2=result表数据*/
        if(row.meter==1){
            url = ctx + "tecmake/chaSheetRelease";
        }else if(row.meter==2){
            url = ctx + "tecmake/chaResultRelease";
        }
        js.confirmPost({
            content:"确定下达此任务吗?",
            url: url,
            async: false,
            data:{
                row:JSON.stringify(row)
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    debugger
                    if(result.data==-2){
                        js.modal.warning(result.msg)
                        js.table.refresh(table);
                    }else{
                        js.modal.success(result.msg)

                        js.table.refresh(table);
                    }
                    refreshNum();
                } else {
                    js.modal.warning(result.msg);
                }
            }
        });
    }


function refreshNum(){
    js.get({
        url: ctx+"tecmake/refreshNum",
        success : function (result){
            if (result.type === web_status.SUCCESS) {
                $("#query-unproNum").val(result.data.unproNum);
                $("#query-unplanNum").val(result.data.unplanNum);
            } else {
                js.modal.warning(result.msg);
            }
        }
    })

}

    function changeMakeSplit(row){

        js.post({
            url: ctx+'tecmake/changeSplit/verify',
            data:{
                sheetId:row.sheetId
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    if(result.data==-1){
                        js.modal.warning(result.msg)
                        js.table.refresh(table);
                    }else{
                        location.href = ctx + 'purch/changeSplit?id=' + row.id;
                    }
                } else {
                    js.modal.warning(result.msg);
                }
            }
        });
    }


</script>
</body>
</html>
